package com.module.prms.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;

import base.dao.GenericHibernateDao;

import com.module.prms.vo.PrmsRoleEntEvent;
import com.module.prms.vo.PrmsRoleFlowList;
import com.module.prms.vo.PrmsUserRole;

public class RoleEntEventDao extends
		GenericHibernateDao<PrmsRoleEntEvent, String> {
	
	private RoleEntEventDao(){}
	
	private static RoleEntEventDao dao;
	
	public synchronized static RoleEntEventDao getInstance(){
		if(dao == null )
			dao = new RoleEntEventDao();
		return dao;
	}

	/**
	 * 获取用户角色的溯源流程、溯源流程操作项
	 * @param roldIds
	 * @return
	 */
	public List<PrmsRoleEntEvent> getRoleEntEvent(Session session,String userRoleIds) {
		String hql = "";
		String in = "";
		
		if(userRoleIds!=null){
//			String userRoleIds = (String) map.get("userRoleIds");
			String roleId = "";
			
			PrmsUserRole prmsUserRole = null;
			
			String id[] = null;

			if (!userRoleIds.equals("")) {
				id = userRoleIds.split("/");
			}
			
			for(int i=0;i<id.length;i++){
				
				roleId = id[i];
				
				if(!roleId.equals("")){
					in = in + "'" + roleId + "',";
				}
			}
		}
		
		in = in.substring(0, in.length()-1);
		
		hql = "from PrmsRoleEntEvent as obj where obj.status !='D' and obj.roleId in (" + in + ") "
				+ " order by obj.roleId,obj.flowId,obj.eventId ";
		
		Query query = session.createQuery(hql.toString());
		
		List<PrmsRoleEntEvent> roleEntEvent = query.list();
		
		return roleEntEvent;
	}
//	
//	
//	public List<PrmsRoleEntEvent> getRoleEntEventByFlowId(Session session,Map<String,Object> map) {
//		String hql = "";
//		
//		if(map!=null){
//			String flowId = (String) map.get("flowId");
//		
//			hql = "from PrmsRoleEntEvent as obj where obj.status !='D' and obj.flowId = " + flowId;
//		
//		}
//		
//		Query query = session.createQuery(hql.toString());
//		
//		List<PrmsRoleEntEvent> roleEntEvent = query.list();
//		
//		return roleEntEvent;
//		
//	}
	
	/**
	 * 企业用户获取本企业溯源流程项
	 * @param session
	 * @param flow
	 * @return
	 */
	public List findRoleEntEventByOrgId(Session session, String roleid){
		String hql="";
//		String roleid = (String) map.get("roleid");
		hql="from PrmsRoleEntEvent as obj where obj.roleId='"+roleid+"'group by obj.flowId";
		Query query = session.createQuery(hql.toString());
		List<PrmsRoleEntEvent> roleEntEventList = query.list();
		List<PrmsRoleFlowList> flowlist = new ArrayList<PrmsRoleFlowList>();
		try{
		for(PrmsRoleEntEvent re:roleEntEventList){
			PrmsRoleFlowList pflow = new PrmsRoleFlowList();
			pflow.setFlowid(re.getFlowId());
			pflow.setFlowName(re.getFlowName());
			pflow.setOrgid(re.getOrgId());
			hql="from PrmsRoleEntEvent as obj where obj.flowId='"+re.getFlowId()+"'and obj.roleId='"+roleid+"'";
			Query query2 = session.createQuery(hql.toString());
			List<PrmsRoleEntEvent> eventlist = query2.list();
			pflow.setPrmsRoleEntEvent(eventlist);
			flowlist.add(pflow);
		}
		}catch(Exception e){
			e.printStackTrace();
		}
			
		return flowlist;
	}
	/**
	 * 根据流程id或者操作id，查询企业的溯源流程操作是否关联有角色
	 * @param session
	 * @param map
	 * @return
	 */
	public boolean findEntEvent(Session session,Map<String,Object> map){
		String hql="";
		String orgid = (String) map.get("flowOptid");
		boolean isTrue = false;
		hql="from PrmsRoleEntEvent as obj where obj.flowId='"+orgid+"' or obj.eventId='"+orgid+"'";
		Query query = session.createQuery(hql.toString());
		try{
			List<PrmsRoleEntEvent> roleEntEventList = query.list();
			if(roleEntEventList.size()>0)
				isTrue = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return isTrue;
	}
	
public static void main (String args[])
{
	RoleEntEventDao rod = new RoleEntEventDao();
	//rod.getroleopt("40288509318e630d01318e6361f50001");
}
	
	
	
}
